package cn.iocoder.yudao.module.erp.controller.admin.contract.contractfilerelation;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.contract.contractfilerelation.vo.ContractFileRelationPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.contract.contractfilerelation.vo.ContractFileRelationRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.contract.contractfilerelation.vo.ContractFileRelationSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.contractfilerelation.ContractFileRelationDO;
import cn.iocoder.yudao.module.erp.service.contractfilerelation.ContractFileRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 合同文件关联")
@RestController
@RequestMapping("/erp/contract-file-relation")
@Validated
public class ContractFileRelationController {

    @Resource
    private ContractFileRelationService contractFileRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建合同文件关联")
    @PreAuthorize("@ss.hasPermission('erp:contract-file-relation:create')")
    public CommonResult<Long> createContractFileRelation(@Valid @RequestBody ContractFileRelationSaveReqVO createReqVO) {
        return success(contractFileRelationService.createContractFileRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同文件关联")
    @PreAuthorize("@ss.hasPermission('erp:contract-file-relation:update')")
    public CommonResult<Boolean> updateContractFileRelation(@Valid @RequestBody ContractFileRelationSaveReqVO updateReqVO) {
        contractFileRelationService.updateContractFileRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同文件关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:contract-file-relation:delete')")
    public CommonResult<Boolean> deleteContractFileRelation(@RequestParam("id") Long id) {
        contractFileRelationService.deleteContractFileRelation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除合同文件关联")
                @PreAuthorize("@ss.hasPermission('erp:contract-file-relation:delete')")
    public CommonResult<Boolean> deleteContractFileRelationList(@RequestParam("ids") List<Long> ids) {
        contractFileRelationService.deleteContractFileRelationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同文件关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:contract-file-relation:query')")
    public CommonResult<ContractFileRelationRespVO> getContractFileRelation(@RequestParam("id") Long id) {
        ContractFileRelationDO contractFileRelation = contractFileRelationService.getContractFileRelation(id);
        return success(BeanUtils.toBean(contractFileRelation, ContractFileRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同文件关联分页")
    @PreAuthorize("@ss.hasPermission('erp:contract-file-relation:query')")
    public CommonResult<PageResult<ContractFileRelationRespVO>> getContractFileRelationPage(@Valid ContractFileRelationPageReqVO pageReqVO) {
        PageResult<ContractFileRelationDO> pageResult = contractFileRelationService.getContractFileRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractFileRelationRespVO.class));
    }

    /*@GetMapping("/export-excel")
    @Operation(summary = "导出合同文件关联 Excel")
    @PreAuthorize("@ss.hasPermission('erp:contract-file-relation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractFileRelationExcel(@Valid ContractFileRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContractFileRelationDO> list = contractFileRelationService.getContractFileRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合同文件关联.xls", "数据", ContractFileRelationRespVO.class,
                        BeanUtils.toBean(list, ContractFileRelationRespVO.class));
    }*/

}